package com.cskaoyan.market.service;


import com.cskaoyan.market.db.domain.MarketCollect;
import com.cskaoyan.market.db.domain.MarketCollectExample;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.mapper.MarketCollectMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.vo.WxMyCollectListbriefVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WxMyCollectServiceImpl implements WxMyCollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public Map<String, Object> listCollects() {
        MarketCollectExample example = new MarketCollectExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<MarketCollect> collects = marketCollectMapper.selectByExample(example);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(new MarketGoodsExample());
        Map<Integer, MarketGoods> goodsMap = idGetGoodsMap(marketGoods);
        List<WxMyCollectListbriefVo> resultListVo = new ArrayList<>();

        for (MarketCollect collect : collects) {
            if (!collect.getDeleted()) {
                MarketGoods marketGood = goodsMap.get(collect.getValueId());
                WxMyCollectListbriefVo resultVo = new WxMyCollectListbriefVo(
                        marketGood.getBrief(), marketGood.getId(), marketGood.getName(),
                        marketGood.getPicUrl(), marketGood.getRetailPrice(), 0,
                        Integer.parseInt(marketGood.getGoodsSn()), collect.getAddTime(), collect.getUpdateTime()
                );
                resultListVo.add(resultVo);
            }
        }

        resultListVo.sort((o1, o2) -> o2.getAddTime().compareTo(o1.getAddTime()));

        PageInfo<WxMyCollectListbriefVo> marketGoodsPageInfo = new PageInfo<>(resultListVo);
        HashMap<String, Object> data = new HashMap<>();
        data.put("list", marketGoodsPageInfo.getList());
        data.put("total", marketGoodsPageInfo.getTotal());
        data.put("pages", marketGoodsPageInfo.getPages());
        data.put("page", marketGoodsPageInfo.getPageNum());
        data.put("limit", marketGoodsPageInfo.getPageSize());

        return data;
    }

    private Map<Integer, MarketGoods> idGetGoodsMap(List<MarketGoods> marketGoods) {
        Map<Integer, MarketGoods> map = new HashMap<>();
        for (MarketGoods marketGood : marketGoods) {
            Integer goodsId = marketGood.getId();
            map.put(goodsId, marketGood);
        }
        return map;
    }

    @Override
    public void addOrDelete(MarketCollect marketCollect) {
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();

        if (marketCollect.getUserId() != null) {
            criteria.andUserIdEqualTo(marketCollect.getUserId());
        }
        if (marketCollect.getValueId() != null) {
            criteria.andValueIdEqualTo(marketCollect.getValueId());
        }
        List<MarketCollect> existingCollects = marketCollectMapper.selectByExample(marketCollectExample);

        if (!existingCollects.isEmpty()) {
            marketCollectMapper.deleteByPrimaryKey(existingCollects.get(0).getId());
        } else {
            marketCollect.setAddTime(LocalDateTime.now());
            marketCollect.setUpdateTime(LocalDateTime.now());
            marketCollectMapper.insertSelective(marketCollect);

        }



    }

}









